require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIResetPop = {};
HelperFunc.SetUIEnv(UIResetPop)
local this = UIResetPop
 
function UIResetPop.Init( _rewards, _func_cb )--todo 应该小写 暂时先大写 
    this.rewards = _rewards
    this.func_cb = _func_cb
    local name,package = getUIName(PanelNames.UIResetPop)
    panelMgr:ShowUI(name,package, this.onCreate, Config.useLocalUI)
end
 
function UIResetPop.onCreate(obj)
    if not this.hasInit then
        this.hasInit = true 
        this.btn_close = obj:GetChild("bgClose") 
        local com_top = obj:GetChild("n45")
        local txt_title = com_top:GetChild("title")
        txt_title.text = getLanguage("CommonPop_Title70")
        this.txt_title = obj:GetChild("title_Text")
        this.txt_pop = obj:GetChild("Pop")
        this.btn_cancel = obj:GetChild("CancelBtn")
        this.btn_confirm = obj:GetChild("ConfirmBtn")
        this.item_sv = obj:GetChild("ItemSv")
        this.item_sv.itemRenderer = this.UpdateListReward
        this.addEvent()
    end

    this.item_sv.numItems = #this.rewards
    local cost_num = dataTable.getDataTableByStringId("t_global", 501 ,"f_int_value")
    this.txt_pop.text = string.format(getLanguage("CommonPop_Tips70"), cost_num)
end

function UIResetPop.addEvent()
    this.btn_close.onClick:Add(this.onClickClose)
    this.btn_cancel.onClick:Add(this.onClickClose)
    this.btn_confirm.onClick:Add(this.onClickConfirm)
end

function UIResetPop.onClickConfirm()
    if this.func_cb ~= nil then
        this.func_cb()
    end
    closeUI(PanelNames.UIResetPop)
end

function UIResetPop.removeEvent()
    this.btn_close.onClick:Clear()
    this.btn_cancel.onClick:Clear()
    this.btn_confirm.onClick:Clear()
end

function UIResetPop.onClickClose()
    closeUI(PanelNames.UIResetPop)
end

function UIResetPop.UpdateListReward( index, obj)
    local item_info = this.rewards[index+1]
    local itemCtr=CtrlManager.GetCtrl(PanelNames.Item)
    local itemData = logicMgr.CostManager.getItemOrEquipDataByIdAndNum(item_info.itemModelId, tonumber(tostring(item_info.num)));
    --itemData.isShowName = true
    itemCtr.initData(obj, itemData)
end

function UIResetPop.LocalListenEvent()
    -- ListenEvent(Config.EventType.ItemResolve,this.ReceiveItemResolveMsg)
end

function UIResetPop.LocalCloseEvent()
    -- CloseEvent(Config.EventType.ItemResolve,this.ReceiveItemResolveMsg)
end

function UIResetPop.OnDestroy()
    this.removeEvent()
    this.rewards = nil
    this.cost_num = nil
    this.func_cb = nil
end

function UIResetPop.OnEnable()
    this.LocalListenEvent()
end

function UIResetPop.OnDisable()
    this.LocalCloseEvent()
end
 
return UIResetPop
